package cc.vicp.djx314.msing.jndi;

import javax.naming.NamingException;

import org.springframework.jndi.JndiObjectFactoryBean;

public class MyJndiObjectFactoryBean extends JndiObjectFactoryBean {
	public static final String TOMCAT_PREFIX = "java:comp/env/";
	
	@Override
	protected Object lookup(String jndiName) throws NamingException {
		Object obj;
		try {
			obj = super.lookup(jndiName);
		} catch (NamingException e) {
			String newJndiName = TOMCAT_PREFIX + getJndiName();
			logger.info("找不到名为" + jndiName + "的jndi对象，尝试寻找名为" + newJndiName + "的对象");
			obj = super.lookup(newJndiName);
		}
		return obj;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected <T> T lookup(String arg0, Class<T> arg1) throws NamingException {
		Object obj;
		try {
			obj = super.lookup(arg0, arg1);
		} catch (Exception e) {
			String newJndiName = TOMCAT_PREFIX + arg0;
			logger.info("找不到名为" + arg0 + "的jndi对象，尝试寻找名为" + newJndiName + "的对象");
			obj = super.lookup(newJndiName, arg1);
		}
		return (T) obj;
	}
}